package com.example.hometown_walker.service.design.stucture.adapter;

public class AdapterMain {
    public static void main(String[] args) {

        HwEarphone hwEarphone = new HwEarphone();//华为耳机

        HwMobile hwMobile = new HwMobile();
        hwEarphone.insert(hwMobile);//插入华为手机听音乐

        XmiMobile xmiMobile = new XmiMobile();
//        hwEarphone.insert(xmiMobile); // 不能直接插入小米手机听音乐

        XmiMobileAdapter xmiMobileAdapter = new XmiMobileAdapter(xmiMobile);
        hwEarphone.insert(xmiMobileAdapter); // 华为耳机通过适配器插入小米手机听音乐
    }
}

/**
 * 华为手机
 */
class HwMobile {

    void play() {
        System.out.println("华为手机开始播放音乐");
    }
}

/**
 * 华为耳机
 */
class HwEarphone {

    void insert(HwMobile hwMobile) {
        System.out.println("耳机接入");
        hwMobile.play();
    }
}

/**
 * 小米手机
 */
class XmiMobile {

    void playMusic() {
        System.out.println("小米手机播放音乐");
    }
}

/**
 * 小米 -> 华为 适配（向谁适配，则继承谁）
 */
class XmiMobileAdapter extends HwMobile {
    private XmiMobile xmiMobile;

    public XmiMobileAdapter(XmiMobile mobile) {
        this.xmiMobile = mobile;
    }

    @Override
    void play() {
        if (xmiMobile != null) {
            xmiMobile.playMusic();
        }
    }
}